package com.rytong.tools.ui.chart;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class LPBackgroundLayout extends View implements Chart {

    // x轴的marginTop
    float xScaleMaginTop_;
    // y轴的marginTop
    float yScaleMaginTop_;
    // y轴的刻度值集合
    List<String> yValueSpace_;
    // x轴的刻度数
    int xValueNum_;
    // y轴文字高度
    int yFontHeight_;

    // 视图的宽
    int backgroundLayoutWidth_;
    // 视图的高
    int backgroundLayoutHeight_;

    // 画笔
    Paint paint_;

    /**
     * 判断是否为画垂直线用的view
     */
    private boolean isLine_;
    /**
     * 垂直直线的x坐标
     */
    private float line_X_coordinate_;
    /**
     * 标志seekbar的滑块是否被touch
     */
    private boolean isTouching_;

    public LPBackgroundLayout(Context context, int num, int fontHeight, boolean isLine, int color) {
        super(context);
        isLine_ = isLine;
        xValueNum_ = num;
        yFontHeight_ = fontHeight;
        paint_ = new Paint();
        paint_.setAntiAlias(true);
        paint_.setColor(color);
        paint_.setStrokeWidth(1);
        if (isLine_) {
            setBackgroundColor(0);
            paint_.setColor(Color.parseColor("#B51B1A"));
        }
    }

    @Override
    public void setXScaleMarginTop(float valueSpace) {
        xScaleMaginTop_ = valueSpace;
    }

    @Override
    public float getXScalemarginTop() {
        return xScaleMaginTop_;
    }

    @Override
    public void setYScaleMarginTop(float valueSpace) {
        yScaleMaginTop_ = valueSpace;
    }

    @Override
    public float getYScaleMarginTop() {
        return yScaleMaginTop_;
    }

    @Override
    public void setYScaleValues(List<String> values) {
        yValueSpace_ = values;
    }

    @Override
    public List<String> getYScaleVaues() {
        return yValueSpace_;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isLine_) {
            drawLine(canvas);
        } else {
            drawGrid(canvas);
        }
    }

    /**
     * 绘制垂直线
     * 
     * @param canvas
     */
    private void drawLine(Canvas canvas) {
        canvas.save();
        canvas.translate(line_X_coordinate_, 0);
        canvas.drawLine(0, 0, 0, backgroundLayoutHeight_, paint_);
        canvas.restore();
    }

    /**
     * 绘制网格
     * 
     * @param canvas
     */
    private void drawGrid(Canvas canvas) {
        int size = yValueSpace_.size();
        // 画水平线
        for (int i = 0; i < size; i++) {
            if (i == 0)
                continue;
            canvas.drawLine(0, backgroundLayoutHeight_ - i * yScaleMaginTop_, backgroundLayoutWidth_,
                    backgroundLayoutHeight_ - i * yScaleMaginTop_, paint_);
        }
        // 画垂直线
        for (int i = 0; i < xValueNum_; i++) {
            canvas.drawLine((float) (xScaleMaginTop_ * (i + 0.5)), backgroundLayoutHeight_,
                    (float) (xScaleMaginTop_ * (i + 0.5)), yFontHeight_, paint_);
        }
    }

    /**
     * 设置seekbar中滑块对应到本view的x坐标
     * 
     * @param line_X_coordinate_
     */
    public void setLine_X_coordinate_(float line_X_coordinate_) {
        this.line_X_coordinate_ = line_X_coordinate_;
        invalidate();
    }

    /**
     * 设置seekbar是否被touch
     * 
     * @param b
     */
    public void setIsTouching(boolean b) {
        this.isTouching_ = b;
    }

    @Override
    public void setXScaleValues(List<String> values) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public List<String> getXScaleValue() {
        // TODO Auto-generated method stub
        return null;
    }

}
